package com.bluedot.www.baiyetest.servlet.baiyetest.servlet;


import com.bluedot.www.baiyetest.servlet.baiyetest.pojo.User;
import com.bluedot.www.baiyetest.servlet.baiyetest.service.ServiceContext;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

/**
 * 测试类
 *
 * @author Guo Zhipeng
 * @date 2021/08/04/17:22
 */
public class DispatcherServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = req.getRequestURI();
        String[] split = url.split("/");
        String serviceName = split[split.length - 2];
        String methodName = split[split.length - 1];
        String id = req.getParameter("id");
        ServiceContext serviceContext = new ServiceContext();
        try {
            User user = serviceContext.execute(serviceName,methodName,id);
            System.out.println("user="+user.toString());
            PrintWriter printWriter= resp.getWriter();
            printWriter.write(user.toString());
            printWriter.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
}
